﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Management.Automation;
using System.IO;
using System.Diagnostics;
using bLogical.BizTalkManagement.Helpers;

namespace bLogical.BizTalkManagement.Cmdlets
{
    [Cmdlet(VerbsCommon.Get, "Marks")]
    public class GetMarks : PSCmdlet
    {
        ProgressRecord _progressRecord;
        #region Parameters

        /// <summary>
        /// The path to where the backup files are located
        /// </summary>
        [Parameter(Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        [ValidateNotNullOrEmpty]
        public string BackupPath { get; set; }
        
        #endregion Parameters

        #region Cmdlet Overrides

        /// <summary>
        /// The ProcessRecord method calls the Process.GetProcesses 
        /// method to retrieve the processes specified by the Name 
        /// parameter. Then, the WriteObject method writes the 
        /// associated processes to the pipeline.
        /// </summary>
        protected override void ProcessRecord()
        {
            WriteObject("**************************************************", false);
            WriteObject("bLogical.BizTalkManagement - Get Marks", false);
            WriteObject("**************************************************", false);

            if (!ValidateInput())
                return;

            DatabaseManager manager = new DatabaseManager();
            
            try
            {
                WriteObject(manager.GetMarks(this.BackupPath),false);
            }
            catch (Exception ex)
            {
                string msg = string.Empty;
                while (ex != null)
                {
                    msg += ex.Message + "\n";
                    ex = ex.InnerException;
                }
                WriteError(new ErrorRecord(new Exception(msg), msg, ErrorCategory.DeviceError, null));
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void manager_OnPercentComplete(object sender, PercentEventArgs e)
        {
            if (_progressRecord == null)
                _progressRecord = new ProgressRecord(1, "Percent complete:", e.PercentComplete.ToString());
            else
                _progressRecord.StatusDescription = e.PercentComplete.ToString();

            WriteProgress(_progressRecord);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void manager_OnComplete(object sender, CompleteEventArgs e)
        {
            WriteObject(e.Message);
        }
        
        #endregion Overrides

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        bool ValidateInput()
        {
            string errors = string.Empty;

            if (String.IsNullOrEmpty(this.BackupPath))
                errors += "The Backup path can not be empty\n";
            else if (!Directory.Exists(this.BackupPath))
                errors += String.Format("The Backup path does not exist [{0}]\n", this.BackupPath);

            if (!String.IsNullOrEmpty(errors))
            {
                WriteError(new ErrorRecord(new ApplicationException(errors), "Parameter error", ErrorCategory.InvalidData, null));
                return false;
            }
            return true;


        }
    }
}
